package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateUnion;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.logic.cross.stronghold.CrossStrongholdActivityRankListModel;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.server.pb.CrossStrongholdPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_WAR_ELIMINATION_UNION_LIST, desc = "")
public class CrossGetEliminateUnionListCmd extends CrossUnionCommand {
    private int activityId;
    private String myUnionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossGetEliminateUnionListReqMsg req = StrongholdWarProto.CrossGetEliminateUnionListReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        myUnionId = req.getUnionId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        // 存联盟消息
        Map<Integer, StrongholdWarProto.EliminateUnionBaseDataMsg.Builder> unionMsgMap = new HashMap<>();
        CrossStrongholdEliminateUnion myUnion = null;
        List<CrossStrongholdEliminateUnion> eliminateUnionList = CrossStrongholdMgr.getEliminateUnionList(activityId, groupId);
        //联盟map容器
        Map<String, CrossStrongholdEliminateUnion> unionMap = new HashMap<>();
        for (CrossStrongholdEliminateUnion union : eliminateUnionList) {
            unionMap.put(union.getUnionUid(), union);
            if (union.getUnionUid().equals(myUnionId)) {
                myUnion = union;
            }
        }
        StrongholdWarProto.CrossGetEliminateUnionListRespMsg.Builder resp = StrongholdWarProto.CrossGetEliminateUnionListRespMsg.newBuilder();
        CrossActivityUnionRankModel model = CrossStrongholdRankMgr.getCrossActivityUnionRankListModel(activityId, groupId, CrossStrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION, true);
        if (model == null || model.getRankList() == null || model.getRankList().isEmpty()) {
            //如果还没开始淘汰赛，那么取据点战排名
            for (CrossStrongholdEliminateUnion union : eliminateUnionList) {
                int rank = CrossStrongholdRankMgr.getCrossActivityUnionRank(activityId, CrossStrongholdActivityMgr.CONDITION_TYPE_WILD_UNION, union.getServerId(), union.getUnionUid());
                union.setRank(rank);
                StrongholdWarProto.EliminateUnionBaseDataMsg.Builder builder = CrossStrongholdPb.parseEliminateUnionMsg(union, rank);
                unionMsgMap.put(union.getId(), builder);
            }
        } else {
            int rank = 0;
            //先获取有排行榜的
            for (UnionActivityRankProto.UnionActivityRankTempMsg.Builder builder : model.getRankListMsg().getRankListBuilderList()) {
                if (unionMap.size() <= 0) {
                    break;
                }
                rank++;
                if (unionMap.containsKey(builder.getUnionUid())) {
                    unionMap.get(builder.getUnionUid()).setRank(rank);
                    unionMap.remove(builder.getUnionUid());
                }
            }
            for (CrossStrongholdEliminateUnion union : unionMap.values()) {
                rank++;
                union.setRank(rank);
            }

            for (CrossStrongholdEliminateUnion union : eliminateUnionList) {
                StrongholdWarProto.EliminateUnionBaseDataMsg.Builder builder = CrossStrongholdPb.parseEliminateUnionMsg(union, union.getRank());
                unionMsgMap.put(union.getId(), builder);
            }
        }
        CrossStrongholdActivityRankListModel rankListModel = CrossStrongholdRankMgr.getCrossActivityRankListModel(activityId, groupId, CrossStrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_USER, true);
        if (rankListModel != null) {
            LinkedList<CrossUserRank> rankList = rankListModel.getRankList();
            if (rankList != null && rankList.size() > 0) {
                CrossUserRank firstUser = rankList.get(0);
                if (firstUser != null) {
                    StrongholdWarProto.EliminateUserBaseInfoMsg.Builder firstMsg = StrongholdWarProto.EliminateUserBaseInfoMsg.newBuilder();
                    firstMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(firstUser.getUserBaseInfo()));
                    resp.setFirstRankUser(firstMsg);
                }
            }
        }
        if (myUnion != null) {
            // 有资格的联盟  走联盟固有排序
            List<Integer> sortList = myUnion.getSortList();
            for (Integer id : sortList) {
                StrongholdWarProto.EliminateUnionBaseDataMsg.Builder builder = unionMsgMap.get(id);
                if (builder != null) {
                    resp.addUnionData(builder);
                }
            }
        }else {
            // 无资格的玩家， 走默认排序
            for (StrongholdWarProto.EliminateUnionBaseDataMsg.Builder builder : unionMsgMap.values()) {
                resp.addUnionData(builder);
            }
        }

        YanQuMessage msg = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_UNION_DATA, resp);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), msg);


    }

    @Override
    public void sendData() {

    }
}
